<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    //实现数组扁平化
    var a = [0, 2, 3, 5, 6, [12, 46, 23, 89, [15, 46, 78, 46]], 8]


    //递归遍历数组实现数组扁平化
    function falt(arr) {
      var result = []

      for (let i = 0; i < arr.length; i++) {
        if (Array.isArray(arr[i])) {
          result = result.concat(falt(arr[i]))
        } else {
          result.push(arr[i])
        }
      }
      return result
    }


    // console.log(falt(a));


    //用toString方法实现数组扁平化
    function shuzu(arr) {
      return arr.toString().split(",").map(function (item) {
        return item
      })
    }

    // console.log(shuzu(a));

    //reduce实现数组扁平化

    function bianping(arr) {

      return arr.reduce(function (pre, next) {
        return pre.concat(Array.isArray(next) ? bianping(next) : next)
      }, [])
    }


    // console.log(bianping(a));
  </script>
</body>

</html>